package com.atguigu.realtime.app;

import com.atguigu.realtime.util.MyKafkaUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/4/17 9:38
 */
abstract public class BaseAppV2 {
    protected abstract void run(StreamExecutionEnvironment env,
                                Map<String, DataStreamSource<String>> streams);
    
    public void init(int port,
                     int parallelism,
                     String checkpointDir,
                     String groupId,
                     String topic,
                     String... otherTopics) {
        System.setProperty("hadoop_user_name", "atguigu");
        // 1. 消费Kafka数据
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(parallelism);
        
        env.setStateBackend(new FsStateBackend("hdfs://hadoop162:8020/gmall1026/" + checkpointDir));
        env.enableCheckpointing(1000 * 20, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(60000);
        
        env.getCheckpointConfig()
            .enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        
        // 传入几个topic就需要得到几个流
        ArrayList<String> topics  = new ArrayList<>();
        topics.add(topic);
        topics.addAll(Arrays.asList(otherTopics));
    
        HashMap<String, DataStreamSource<String>> streams = new HashMap<>();
        for (String t : topics) {
            DataStreamSource<String> stream = env.addSource(MyKafkaUtil.getKafkaSource(groupId, t));
            streams.put(t, stream);
        }
        
        run(env, streams);
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
